//package com.binguo.api.service;
//
//import com.binguo.api.businessmodel.DiscountOrder;
//import com.binguo.model.mapper.*;
//import com.binguo.model.model.*;
//import groovy.util.IFileNameFinder;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//import tk.mybatis.mapper.entity.Condition;
//
//import javax.annotation.Resource;
//import java.math.BigDecimal;
//import java.util.*;
//
//@Service
//public class DiscountService {
//
//    static Map<Integer, Integer> dayOfWeekMap = new HashMap<>();
//    static {
//        dayOfWeekMap.put(1, 7);
//        dayOfWeekMap.put(2, 1);
//        dayOfWeekMap.put(3, 2);
//        dayOfWeekMap.put(4, 3);
//        dayOfWeekMap.put(5, 4);
//        dayOfWeekMap.put(6, 5);
//        dayOfWeekMap.put(7, 6);
//    }
//
//    @Resource
//    private DiscountMapper discountMapper;
//
//    @Resource
//    private CustomerExtendMapper customerExtendMapper;
//
//    @Resource
//    private PayCardMapper payCardMapper;
//
//    @Resource
//    private ShopHoursConfigMapper shopHoursConfigMapper;
//    @Resource
//    private ShopConfigExtendMapper shopConfigExtendMapper;
//
//    public DiscountOrder discount(Integer shopId, Integer customerId, Long totleFee){
//        DiscountOrder discountOrder = new DiscountOrder();
//        //查询优惠信息
//        ShopDiscount shopDiscount = discountMapper.selectByShopId(shopId);
//        Integer groupID = null;
//
//        if (shopDiscount != null) {
//            Condition condition = new Condition(CustomerExtend.class);
//            condition.or().andEqualTo("shopId", shopId)
//                    .andEqualTo("customerId", customerId);
//            List<CustomerExtend> customerExtendList = customerExtendMapper.selectByCondition(condition);
//
//            String userGroupId = null;
//            if (customerExtendList != null && customerExtendList.size() > 0) {
//                CustomerExtend customerExtend = customerExtendList.get(0);
//
//                userGroupId = customerExtend.getGroupId().toString();
//            }
//            //查询授权
//            if (StringUtils.hasText(shopDiscount.getProductCategoryIdList())) {
//                if ("-1".equals(shopDiscount.getProductCategoryIdList())) {//1、全部菜品
//
//                    String userGroupIdList = shopDiscount.getUserGroupIdList();
//
//                    boolean allowed = false;
//                    if ("-1".equals(userGroupIdList)) {//2、全部用户组
//                        allowed = true;
//                    }else if (StringUtils.hasText(userGroupIdList)) {
//                        if (userGroupIdList.contains(",")) {
//                            String[] userGroupArray = userGroupIdList.split(",");
//
//                            for (String s : userGroupArray) {
//                                if (s.equals(userGroupId)) {
//                                    allowed = true;
//                                    break;
//                                }
//                            }
//                        } else {
//                            if (userGroupIdList.equals(userGroupId.toString())) {
//                                allowed = true;
//                            }
//                        }
//                    }
//                    if (allowed) {//有资格
//                        boolean timeAllow = false;
//
//                        String discountTime = shopDiscount.getDiscountTime();
//
//                        if ("-1".equals(discountTime)) {//3、全部时间
//                            timeAllow = true;
//                        } else if (StringUtils.hasText(discountTime)) {
//                            //3、优惠时间,周几
//                            int dayOfWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
//                            String chineseDayOfWeek = String.valueOf(dayOfWeekMap.get(dayOfWeek));
//
//                            if (discountTime.contains(",")) {
//                                String[] discountTimeArray = discountTime.split(",");
//                                for (String s : discountTimeArray) {
//                                    if (s.equals(chineseDayOfWeek)) {
//                                        timeAllow = true;
//                                        break;
//                                    }
//                                }
//                            } else {
//                                if (discountTime.equals(chineseDayOfWeek)) {
//                                    timeAllow = true;
//                                }
//                            }
//                        }
//
//
//                        if (timeAllow) {
//                            boolean periodAllow = false;
//                            String discountPeriod = shopDiscount.getDiscountPeriod();
//
//                            if ("-1".equals(discountPeriod)) {//4。全部时段
//                                periodAllow = true;
//                            } else if (StringUtils.hasText(discountPeriod)) {
//                                if (discountPeriod.contains(",")) {
//                                    String[] disPeriodArray = discountPeriod.split(",");
//
//                                    for (String s : disPeriodArray) {
//                                        periodAllow = isPeriodAllow(s);
//
//                                        if (periodAllow == true) {
//                                            break;
//                                        }
//                                    }
//                                } else {
//                                    periodAllow = isPeriodAllow(discountPeriod);
//                                }
//                            }
//
//                            if (periodAllow) {
//                                boolean ruleAllow = false;
//                                Integer discountType = shopDiscount.getDiscountType();
//                                Integer rule = shopDiscount.getRule();
//                                Integer ruleType = shopDiscount.getRuleType();
//                                Integer value = shopDiscount.getValue();
//
//                                if (discountType != null) {//1:满折 2:满减
//                                    if (totleFee > rule) {
//                                        ruleAllow = true;
//                                    }
//                                }
//
//                                long resultFee = 0;
//
//                                if (ruleAllow) {
//                                    Condition scec = new Condition(ShopConfigExtend.class);
//                                    scec.or().andEqualTo("shopId", shopId);
//                                    List<ShopConfigExtend> shopConfigExtendList = shopConfigExtendMapper.selectByCondition(scec);
//
//                                    ShopConfigExtend shopConfigExtend = null;
//                                    if (shopConfigExtendList != null && shopConfigExtendList.size() > 0) {
//                                        shopConfigExtend = shopConfigExtendList.get(0);
//                                    }
//                                    Integer discountHandleType = null;
//                                    Integer discountPriceUnit = null;
//                                    if (shopConfigExtend != null) {
//                                        discountHandleType = shopConfigExtend.getDiscountHandleTyp();
//                                        discountPriceUnit = shopConfigExtend.getDiscountPriceUnit();
//                                    }
//
//                                    if (discountType == 1) {//1:满折 2:满减
//                                        BigDecimal subFee = new BigDecimal(totleFee * (100 - value)).divide(new BigDecimal(100));//单位分
//
//                                        if (subFee.intValue() > shopDiscount.getDiscountTop()) {//封顶金额配置
//                                            subFee = new BigDecimal(shopDiscount.getDiscountTop());
//                                        }
//
//                                        resultFee = totleFee - subFee.longValue();
//                                    } else if (discountType == 2) {//2:满减
//                                        long subFee = 0;
//                                        if (ruleType != null) {
//                                            if (ruleType == 1) {//1:每满 2:总满
//                                                long count = totleFee / rule;
//
//                                                subFee = count * value;
//                                            } else if (ruleType == 2) {
//                                                subFee = value;
//                                            }
//
//                                            if (subFee > shopDiscount.getDiscountTop()) {
//                                                subFee = shopDiscount.getDiscountTop();
//                                            }
//                                        }
//
//                                        resultFee = totleFee - subFee;
//                                    }
//
//
//                                    if (discountHandleType != null) {//1:四舍五入 2:只入不舍
//                                        if (discountHandleType == 1) {
//                                            if (discountPriceUnit == 1) {//1:分 2:角 3:元
//                                                long temp = resultFee % 10;
//                                                if (temp < 5) {
//                                                    resultFee = resultFee - temp;
//                                                } else {
//                                                    if (resultFee > 10) {
//                                                        resultFee = resultFee - temp + 10;
//                                                    }
//                                                }
//                                            } else if (discountPriceUnit == 2) {
//                                                long temp = resultFee % 100;
//                                                if (temp < 50) {
//                                                    resultFee = resultFee - temp;
//                                                } else {
//                                                    if (resultFee > 100) {
//                                                        resultFee = resultFee - temp + 100;
//                                                    }
//                                                }
//
//                                            }
//                                        } else if (discountHandleType == 2) {//2:只入不舍
//                                            if (discountPriceUnit == 1) {//1:分 2:角 3:元
//                                                long temp = resultFee % 10;
//                                                if (resultFee > 10) {
//                                                    resultFee = resultFee - temp + 10;
//                                                }
//                                            } else if (discountPriceUnit == 2) {
//                                                long temp = resultFee % 100;
//                                                if (resultFee > 100) {
//                                                    resultFee = resultFee - temp + 100;
//                                                }
//
//                                            }
//                                        }
//
//                                    }
//                                    discountOrder.setNote(shopDiscount.getDiscountName());
//                                    discountOrder.setTotalFee(resultFee);
//
//                                    if (resultFee < 0) {
//                                        discountOrder.setTotalFee(0l);
//                                    }
//                                    return discountOrder;
//                                }
//                            }
//                        }
//                    }
//
//                } else {
//                    //TODO
//                    return null;
//                }
//            }
//        }
//
//        return null;
//
//    }
//
//    private boolean isPeriodAllow(String period){
//        boolean periodAllow = false;
//        ShopHoursConfig shopHoursConfig =
//                shopHoursConfigMapper.selectByPrimaryKey(Integer.valueOf(period));
//
//        Date endTime = shopHoursConfig.getEndTime();
//        Date startTime = shopHoursConfig.getStartTime();
//        Calendar current = Calendar.getInstance();
//        Calendar endCalendar = Calendar.getInstance();
//        endCalendar.setTime(endTime);
//        Calendar startCalendar = Calendar.getInstance();
//        startCalendar.setTime(startTime);
//
//        if ((current.get(Calendar.HOUR_OF_DAY) > startCalendar.get(Calendar.HOUR_OF_DAY))
//                &&
//                (current.get(Calendar.MINUTE) > startCalendar.get(Calendar.MINUTE))
//                &&
//                (current.get(Calendar.HOUR_OF_DAY) < endCalendar.get(Calendar.HOUR_OF_DAY))
//                &&
//                (current.get(Calendar.MINUTE) < endCalendar.get(Calendar.MINUTE))
//                ) {
//            periodAllow = true;
//        }
//
//        return periodAllow;
//    }
//}
